package com.danco.sn.model.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.danco.sn.apientities.ICommunity;

public class CommunityDAO extends GenericDAO<ICommunity> {

	private static final String DATE_CREATE = "dateCreate";
	private static final String COMMUNITY_CITY = "communityCity";
	private static final String COMMUNITY_NAME = "communityName";

	public CommunityDAO() {
		super(ICommunity.class);
	}

	@SuppressWarnings("unchecked")
	public List<ICommunity> searchCommunity(Session session, String name,
			String city, Date birthday) throws HibernateException {

		Criteria criteria = session.createCriteria(ICommunity.class);
		if (name != null) {
			criteria.add(Restrictions.like(COMMUNITY_NAME, name.trim()));
		}
		if (city != null) {
			criteria.add(Restrictions.like(COMMUNITY_CITY, city));
		}
		if (birthday != null) {
			criteria.add(Restrictions.eq(DATE_CREATE, birthday));
		}
		
		List<ICommunity> listCommunity = criteria.list();

		if(name==null && city==null && birthday==null){
			return null;
		}

		return listCommunity;
	}

}
